package com.dong.rabbitmq.controller;

import com.dong.rabbitmq.config.ConfirmConfig;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author dongjian
 */
@Slf4j
@RestController
@RequestMapping("/confirm")
public class ProducerController {

    @Resource
    private RabbitTemplate rabbitTemplate;


    @ApiOperation("发送消息到confirm.queue队列")
    @GetMapping("/sendMessage/{message}")
    public void sendMessageConfirm(@PathVariable String message) {

        log.info("发送消息内容: {}, key1", message);
        CorrelationData correlationData = new CorrelationData("1");
        rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE_NAME, ConfirmConfig.CONFIRM_ROUTING_KEY, message, correlationData);

        // 发送一条不可达的信息
        CorrelationData correlationData2 = new CorrelationData("2");
        String s = message + "嘻嘻";
        log.info("发送消息内容: {}, key12", s);
        rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE_NAME, ConfirmConfig.CONFIRM_ROUTING_KEY + "2", s, correlationData2);
    }
}
